package org.belkodevelop.controller;


import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.belkodevelop.controller.helper.AuthUtils;
import org.belkodevelop.form.RegistrationForm;
import org.belkodevelop.form.validator.RegistrationValidator;
import org.belkodevelop.registration.dto.RegistrationStatus;
import org.belkodevelop.user.domain.User;
import org.belkodevelop.user.exception.UserExistsException;
import org.belkodevelop.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class RegistrationController {
    
    private final Log log = LogFactory.getLog(getClass());   
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private RegistrationValidator registrationValidator;
    
    @Autowired
    private LocaleResolver localeResolver;
    
    @Autowired
    private MessageSource messageSource;
    
    @Autowired
    private AuthUtils authUtils;
        
    @RequestMapping(value = "/registration", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView registrationView() {
        return new ModelAndView("registration");
    }
    
    @RequestMapping(value = "/api/registration.json", method = RequestMethod.POST)
    @ResponseBody
    public RegistrationStatus registration(RegistrationForm form, 
                                           BindingResult result, 
                                           HttpServletRequest request,
                                           HttpServletResponse response) {
       
        registrationValidator.validate(form, result, request);
        if (result.hasErrors()) {
            Locale locale = localeResolver.resolveLocale(request);
            return new RegistrationStatus(true, result.getFieldErrors(), locale, messageSource);
        }
        
        User user = form.getUser();
        String login = user.getLogin();
        String password = user.getPassword();
        
        try {
            userService.registerUser(user);
        } catch (UserExistsException ex) {
            log.warn("User exists!", ex);
            return new RegistrationStatus(true);
        }
        

        try {
            authUtils.login(login, password, true, request, response);
        } catch (AuthenticationException ex) {
            return new RegistrationStatus(true);
        }
        return new RegistrationStatus(false);
    }

}
